#!/usr/bin/perl -w
#  
#
# 
use warnings;
use strict;
#
sub Positions;
# Define the DNA sequence 
our $sequence = "CGTACTAGTCATGGGTATGCTAGCTAGTCTTTACCTATTGGACCTATGGAAAGCCATGAGTGGAATTC";
# Define the list of sequences
our @TBFSArray = ("CGT","Y","CGCTA","AGCTAGTC","CGTA","ACCTGATTTTAGAA","GGAAAGCCA"); 
# Fill an array with the positions of the sequences.
my  @test = @{Positions($sequence,\@TBFSArray)}; 
foreach ( my $ i =0; $i <$#TBFSArray+1; $i++){
    print "######".$test[$i]."\n";
}  
#


sub Positions{
    # Get the sequence that was passed as a reference 
    my @seq = split(//, shift(@_));
    # Get the array of TFBSs
    my $refTFBS = shift(@_);
    # Dereference the array
    my @array = @{$refTFBS}; 
    # Get the number of elements in the DNA sequence
    my $numSeq  = $#seq; 
    # Get the number of TFBSs
    my $numTFBS = $#array;
    
    # Declare the array
    my @returned = (); 
    # Loop through the list of TFBS
    for (my $index1 =0; $index1<= $numTFBS; $index1++){
    	# Make an array of each TFBS
        my @TFBS = split(//,$array[$index1]);
        my $numNuc =  $#TFBS;
        # Use a variable to keep track of whether the TFBS is found
        my $found = 1; 
        # loop through the nucleotides of the DNA sequence,
        # from the first one until the last minus the length of the TFBS
       	my $index2 = 0;
        while ($index2<=$numSeq-$numNuc){
        	# Assume first that the TFBS is present in the DNA
        	$found =1;
        	# loop through the TFBS nucleotides
        	my $index3 = 0;
        	while (($index3<= $numNuc)){
        		    # if one nucleotide is different, set $found to 0
   	                if($seq[$index2+$index3] ne $TFBS[$index3]){
   	        	        $found = 0;  	        	     
   	                } 
                $index3++;
            }
            # break the loop over DNA sequence if the TFBS is found. 
            last if ($found);
            $index2++;   
        } 
        # add the position in the DNA 
        if ($found){
        	push(@returned,"Position of TFBS \"@TFBS\" -> $index2"); 
        }
        else{
        	push(@returned,"Position of TFBS \"@TFBS\" -> not found"); 
        }
    } 
    return \@returned;
}
